package org.uim.cricket.util.web;

import java.io.*;
import java.net.*;
import java.util.Properties;

public class WebHTMLReader {

	public static final String PROXY_CONFIG_FILENAME = "./proxy-config.properties";
	
	public static Proxy proxy = null;
	private static String authorization = null;
	
	private String url = null;

	static {
		try {
			if (new File(PROXY_CONFIG_FILENAME).exists()) {
				loadProxyFromPropertyFile(PROXY_CONFIG_FILENAME);
			}
		} catch (IOException ex) {
			
		}
	}
	
	private static void loadProxyFromPropertyFile(String propertyFile) throws IOException {
		
		Properties prop = new Properties();
		prop.load(new FileReader(propertyFile));
		
		String type = prop.getProperty("proxy.type");
		Proxy.Type ptype = Proxy.Type.DIRECT;
		
		if (type != null) {
			if (type.equalsIgnoreCase("NO_PROXY")) {
				proxy = null;
			} else if (type.equalsIgnoreCase("HTTP")) {
				ptype = Proxy.Type.HTTP;
			} else if (type.equalsIgnoreCase("SOCKS")) {
				ptype = Proxy.Type.SOCKS;
			} else if (type.equalsIgnoreCase("DIRECT")) {
				ptype = Proxy.Type.DIRECT;
			} else {
				proxy = null;
				return;
			}
		} else {
			proxy = null;
			return;
		}
		
		String phost = prop.getProperty("proxy.host");
		if (phost == null || phost.length() == 0) {
			proxy = null;
			return;
		}
		
		String ppword = prop.getProperty("proxy.port");
		if (ppword == null || ppword.length() == 0) {
			ppword = "0";
		}
		
		setProxyDetails(ptype, phost, Integer.parseInt(ppword)); 
		
		String ppuname = prop.getProperty("proxy.username");
		if (ppuname == null) ppuname = "";
		String ppuword = prop.getProperty("proxy.password");
		if (ppuword == null) ppuword = "";
		if (ppuname.length() > 0 || ppuword.length() > 0) {
			setProxyAuthorization(ppuname, ppuword);
		}
		
	}
	
	public static void setProxyDetails(Proxy.Type proxyType, String proxyHost, int proxyPort) {
		proxy = new Proxy(proxyType, new InetSocketAddress(proxyHost, proxyPort));
	}
	
	public static void setProxyAuthorization(String username, String password) {
		authorization = new String(Base64.encode(new String(username + ":" + password).getBytes()));
	}
	
	public static void setNoProxy() {
		proxy = null;
	}
	
	public WebHTMLReader(String theURL) {
		this.url = theURL;
	}

	public StringBuilder readPage() throws MalformedURLException, IOException {
		StringBuilder sb = new StringBuilder();

		URLConnection connection = null;
		URL urlh = new URL(url);
		if (proxy == null) {
			connection = urlh.openConnection();
		} else {
			connection = urlh.openConnection(proxy);
		}
		if (authorization != null) {
			connection.setRequestProperty("Proxy-Authorization", "Basic " + authorization);
		}
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				connection.getInputStream()));
		String line = null;

		while ((line = reader.readLine()) != null) {
			sb.append(line).append('\n');
		}

		reader.close();
		connection = null;

		return sb;

	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUrl() {
		return url;
	}

}
